// 状态查询statequery的控制器
angular.module('statequery.controller', ['statequery.service'])
    .controller("StatequeryCtrl", function ($scope, ionicDatePicker, $ionicModal, $rootScope, $http, $filter,$timeout) {
//配置时间日历
        var start = {
            callback: function (val) {  //Mandatory
                console.log('Return value from the datepicker popup is : ' + val, new Date(val));
                $scope.starttime = $filter("date")(new Date(val), "yyyy-MM-dd");
                $scope.st =val;
            },
            disabledDates: [            //Optional
            ],
            from: new Date(2000, 1, 1), //Optional
            to: new Date(2128, 10, 30), //Optional
            inputDate: new Date(),      //Optional
            mondayFirst: true,          //Optional
            disableWeekdays: [""],       //Optional
            closeOnSelect: false,       //Optional
            templateType: 'popup'       //Optional
        };
        $scope.start = function () {
            console.log("开始");
            ionicDatePicker.openDatePicker(start);

        };
        var end = {
            callback: function (val) {  //Mandatory
                $scope.endtimer = $filter("date")(new Date(val), "yyyy-MM-dd");
                $scope.et=val;
                console.log($scope.st);
                console.log($rootScope.et);
                if($scope.et - $scope.st < 0){
                    alert('日期输入错误，开始时间必须小于结束时间');
                    console.log('日期输入错误，开始时间必须小于结束时间');
                    $scope.endtimer=""
                }
            },
            disabledDates: [            //Optional
            ],
            from: new Date(2000, 1, 1), //Optional
            to: new Date(2128, 10, 30), //Optional
            inputDate: new Date(),      //Optional
            mondayFirst: true,          //Optional
            disableWeekdays: [""],       //Optional
            closeOnSelect: false,       //Optional
            templateType: 'popup'       //Optional
        }
        $scope.end = function () {
            ionicDatePicker.openDatePicker(end);
        };


        //接口
        $scope.clientSideList = [];
        $rootScope.clientSideList = []
        var CCSessionId = localStorage.getItem("CCSessionId");
        var url = $rootScope.myglobal.serverurl + '/busistatus/business-status/new.json?CCSessionId=' + CCSessionId

        $scope.init = function () {
            $http({
                method: "get",
                url: url,
            })
                .success(function (data) {
                    //先判断error是否为null或空， 如果error非null，则获取信息失败，报出错误信息, 如果为空，再看下面返回信息
                    if (data.error !== null && data.error !== "" && data.error !== 'undefined') {
                        angular.forEach(data, function (item) {
                            $scope.temp = {key: item[5], value: item[6]}
                            //console.log($scope.temp);
                            $scope.clientSideList.push($scope.temp)
                        })

                    } else {
                        alert(data.error)
                    }
                    var all = {key: "", value: "全部"}
                    $scope.clientSideList.unshift(all);
                })
                .error(function (data) {
                })
        }
        $scope.init()


//业务选择模版
        //单选

        $scope.data = {clientSide: '全部'};
        $ionicModal.fromTemplateUrl('templates/businesschoose.html', {scope: $scope})
            .then(function (modal) {$scope.modal = modal;});
        $scope.createContact = function () {
            $scope.value = $scope.data.clientSide
            console.log($scope.value);
            console.log($scope.clientSideList); //数据
            angular.forEach($scope.clientSideList, function (item) {
                if (item.value == $scope.value) {$rootScope.businessname = item.key}
            })
            console.log($rootScope.businessname);
            $scope.modal.hide();
        };


        //查询状态列表
        $scope.search = function (frist) {
            console.log(frist);
            var businessName;
            var starttime;
            var endtimer;
            if (frist == undefined) {
                console.log("选择的参数");
                businessName = $rootScope.businessname;
                starttime = $scope.starttime;
                endtimer = $scope.endtimer;
            } else {
                console.log("默认参数");
                businessName = frist.businessname;
                starttime = frist.starttime;
                endtimer = frist.endtimer;
            }

            var CCSessionId = localStorage.getItem("CCSessionId");
            var url = $rootScope.myglobal.serverurl + '/busistatus/business-status.json?CCSessionId=' + CCSessionId;
            console.log(businessName);
            console.log(starttime);
            console.log(endtimer);
            $http({
                method: "get",
                url: url,
                params: {
                    businessName: businessName,     //13.1中获取（第一次打开时传空串）
                    startDate: starttime,        //YYYY-MM-DD
                    endDate: endtimer, //要有默认值（最近一个月）
                    pageSize: 50
                }
            })
                .success(function (data) {
                    console.log(data);

                    angular.forEach(data.resultList, function (item) {
                        //     1和2为定期，其他为即时
                        if (item[0].backup1 == 1 || item[0].backup1 == 2) {
                            item[0].backup1 = "定期";
                        } else {
                            item[0].backup1 = "即时";
                        }
                        if(item[0].description.length>10){
                            item[0].description= item[0].description.split(":",[1]).toString();
                            console.log(item[0].description);
                        }
                        //description 若不为空，描述信息则取此字段，若为空，则描述信息根据status获取
                        if (item[0].description == "") {
                            switch (item[0].status) {
                                case 0:
                                    stateStr = "发起"
                                    break;
                                case 1:
                                    stateStr = "内部审批中"
                                    break;
                                case 2:
                                    stateStr = "待银行预处理"
                                    break;
                                case 3:
                                    stateStr = "送达银行失败"
                                    break;
                                case 4:
                                    stateStr = "送达银行主机失败"
                                    break;
                                case 5:
                                    stateStr = "待银行主机处理"
                                    break;
                                case 65:
                                    stateStr = "本地处理失败"
                                    break;
                                case 7:
                                    stateStr = "完成"
                                    break;
                                case 8:
                                    stateStr = "执行中"
                                    break;
                            }
                            item[0].description = stateStr;
                        }
                    })
                    $scope.ztcxlist = data.resultList;
                })
                .error(function (data) {
                })

        }


//第一次进来时候执行一下（第一次打开时businessname传空串）
        var t = new Date();
        var l = t;
        l = $filter("date")(new Date(), "yyyy-MM-dd");
        //        往前推一个月
        t.setMonth(t.getMonth() - 1);
        t = $filter("date")(new Date(t), "yyyy-MM-dd");
        //默认时间一个月
        var defaultstart=document.getElementsByClassName("defaultstart")[0]
        var defaultend=document.getElementsByClassName("defaultend")[0]
        var defaultall=document.getElementsByClassName("defaultall")[0]
        $timeout(function () {
            defaultstart.innerHTML =t;
            defaultend.innerHTML =l;
            defaultall.innerHTML ="全部";
            console.log(defaultstart);
            console.log(defaultend);
        },100)


        console.log(t);
        console.log(l);
        var frist = {businessname: "", starttime: t, endtimer: l}
        $scope.search(frist);


        //下拉刷新列表
        $scope.doRefresh = function () {
            console.log("下拉刷新");
            var CCSessionId = localStorage.getItem("CCSessionId");
            var url = $rootScope.myglobal.serverurl + '/busistatus/business-status.json?CCSessionId=' + CCSessionId;
            console.log($rootScope.businessname);
            console.log($scope.starttime);
            console.log($scope.endtimer);
            $http({
                method: "get",
                url: url,
                params: {
                    businessName: $rootScope.businessname,     //13.1中获取（第一次打开时传空串）
                    startDate: $scope.starttime,        //YYYY-MM-DD
                    endDate: $scope.endtimer, //要有默认值（最近一个月）
                    pageSize: 50
                }
            })
                .success(function (data) {
                    console.log(data);
                    //     1和2为定期，其他为即时
                    angular.forEach(data.resultList, function (item) {
                        if (item[0].backup1 == 1 || item[0].backup1 == 2) {
                            item[0].backup1 = "定期";
                        } else {
                            item[0].backup1 = "即时";
                        }
                        if(item[0].description.length>10){
                            item[0].description= item[0].description.split(":",[1]).toString();
                            console.log(item[0].description);
                        }
                        //description 若不为空，描述信息则取此字段，若为空，则描述信息根据status获取
                        if (item[0].description == "") {
                            switch (item[0].status) {
                                case 0:
                                    stateStr = "发起"
                                    break;
                                case 1:
                                    stateStr = "内部审批中"
                                    break;
                                case 2:
                                    stateStr = "待银行预处理"
                                    break;
                                case 3:
                                    stateStr = "送达银行失败"
                                    break;
                                case 4:
                                    stateStr = "送达银行主机失败"
                                    break;
                                case 5:
                                    stateStr = "待银行主机处理"
                                    break;
                                case 65:
                                    stateStr = "本地处理失败"
                                    break;
                                case 7:
                                    stateStr = "完成"
                                    break;
                                case 8:
                                    stateStr = "执行中"
                                    break;
                            }
                            item[0].description = stateStr;
                        }

                    })
                    $scope.ztcxlist = data.resultList;
                })
                .error(function (data) {
                })


                .finally(function () {
                    $scope.$broadcast('scroll.refreshComplete');
                });


        };


    })



